package com.yuyou.train.lesson.controller;

import com.yuyou.train.lesson.vo.req.ReqResExamItemUpdate;
import com.yuyou.train.lesson.vo.req.ReqResExamUpdate;
import com.yuyou.train.service.api.lesson.dto.ExamItemDTO;
import com.yuyou.train.service.api.lesson.dto.ExamQuestionDTO;

import com.yuyou.train.util.bean.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.yuyou.train.lesson.service.IResourceExamItemService;
import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.vo.req.ReqResExamAdd;
import com.yuyou.train.lesson.vo.req.ReqResExamItemAdd;
import com.yuyou.train.lesson.vo.resp.RespResExamDetail;
import com.yuyou.train.service.api.lesson.dto.ExamPageDTO;
import com.yuyou.train.service.api.lesson.entity.ResourceExam;
import com.yuyou.train.service.api.lesson.entity.ResourceExamItem;
import com.yuyou.train.util.tool.DefindBeanUtils;

import javax.servlet.http.HttpServletRequest;

@Validated
@RestController
@RequestMapping(value="/api/exam")
public class ExamController extends BaseController{

	
	@Autowired
	private IResourceExamService resourceExamService;
	
	@Autowired
	private IResourceExamItemService resourceExamItemService;
	
	
	@ApiOperation(value="新增试卷")
	@RequestMapping(value="/addExam",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addExam(@Validated @ModelAttribute ReqResExamAdd reqResExamAdd,
														   HttpServletRequest request){
		LoginRedisObj loginObj = this.getUserObj(request);
		ResourceExam resourceExam = new ResourceExam();
		DefindBeanUtils.copyProperties(reqResExamAdd, resourceExam);
		resourceExam.setCreateUser(loginObj.getUserId());
		ResBoolSimpleInfo<String> res = resourceExamService.addExam(resourceExam);
		return ReturnResult.success(res);
	}

	@ApiOperation(value="编辑试卷")
	@RequestMapping(value="/updateExam",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addExam(@Validated @ModelAttribute ReqResExamUpdate reqResExamUpdate){
		ResourceExam resourceExam = new ResourceExam();
		DefindBeanUtils.copyProperties(reqResExamUpdate, resourceExam);
		ResBoolSimpleInfo<String> res = resourceExamService.updateExam(resourceExam);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="删除试卷")
	@ApiImplicitParam(name = "id", value = "主键", paramType="query",required = true)
	@RequestMapping(value="/delExam",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> delExam(@NotBlank (message="id不能为空") String id){
		ResBoolSimpleInfo res=resourceExamService.delExam(id);
		return ReturnResult.success(res);
	}


	@ApiOperation(value="批量更改试卷状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "试卷ID列表（多个逗号分隔）", paramType="query", required = true),
			@ApiImplicitParam(name = "isUse", value = "是否启用：0：否，1：是", paramType="query",required = true)}
	)
	@RequestMapping(value="/batchChangeExamStatus" ,method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> batchChangeExamStatus(@NotEmpty(message="试卷ID列表(ids)不能为空") @RequestParam(value="ids") List<String> ids,
															     @NotEmpty (message="状态不能为空") @RequestParam(value="isUse") String isUse){

		ResBoolSimpleInfo resBoolSimpleInfo = resourceExamService.batchUpdateStatus(ids, isUse);
		return ReturnResult.success(resBoolSimpleInfo);
	}


	@ApiOperation(value="试卷详情")
	@ApiImplicitParam(name = "id", value = "试卷id", paramType="query",required = true)
	@RequestMapping(value="/detailExam",method=RequestMethod.POST)
	public ReturnResult<RespResExamDetail> detailQues(
			@NotBlank (message="id不能为空") String id){	
		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(id);
		RespResExamDetail resp = new RespResExamDetail();
		DefindBeanUtils.copyProperties(resourceExam, resp);	
		return ReturnResult.success(resp);
	}


	@ApiOperation(value="试卷的题目明细")
	@ApiImplicitParam(name = "id", value = "试卷id", paramType="query",required = true)
	@RequestMapping(value="/examItemList",method=RequestMethod.POST)
	public ReturnResult<ExamItemDTO> examItemList(@NotBlank (message="id不能为空") String id){

		ExamItemDTO examItemDTO =  resourceExamItemService.findExamItemWithQuestionByExamId(id);
		return ReturnResult.success(examItemDTO);
	}
	
	@ApiOperation(value="试卷分页")
	@ApiImplicitParams({
	@ApiImplicitParam(name = "name", value = "试卷名称", paramType="query", required = false),
	@ApiImplicitParam(name = "isUse", value = "是否启用：0：否，1：是", paramType="query",required = false)}
	)
	@RequestMapping(value="/findExamPage",method=RequestMethod.POST)
	public ReturnResult<Pagination<ExamPageDTO>> findQuesPage(@Validated @ModelAttribute PageParams pageParams,
															  String name,
															  String isUse){
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("name", name);
		conditions.put("isUse", isUse);
		Pagination<ExamPageDTO> queryPgn = new Pagination<ExamPageDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total = resourceExamService.findExamPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<ExamPageDTO> list = resourceExamService.findExamPageList(queryPgn);
			queryPgn.setList(list);
		}	
		return ReturnResult.success(queryPgn);
	}
	
	@ApiOperation(value="新增试卷明细")
	@RequestMapping(value="/addExamItem",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> addExamItem(@Validated @ModelAttribute ReqResExamItemAdd reqResExamItemAdd){

		ResBoolSimpleInfo res = resourceExamItemService.addExamItem(reqResExamItemAdd);
		return ReturnResult.success(res);
	}


	@ApiOperation(value="批量新增试卷明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "resourceExamId", value = "试卷id", paramType="query",required = true),
			@ApiImplicitParam(name = "resourceQuestionIds", value = "题目ID列表（多个逗号分隔）", paramType="query", required = true)}
	)
	@RequestMapping(value="/batchAddExamItem",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> batchAddExamItem(@NotEmpty (message="resourceExamId 不能为空") @RequestParam(value="resourceExamId") String resourceExamId,
															@NotEmpty(message="题目ID列表(ids)不能为空") @RequestParam(value="resourceQuestionIds") List<String> resourceQuestionIds){

		ResBoolSimpleInfo res = resourceExamItemService.batchAddExamItem(resourceExamId, resourceQuestionIds);
		return ReturnResult.success(res);
	}

	
	@ApiOperation(value="更改试卷明细")
	@RequestMapping(value="/updateExamItem",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> updateExamItem(@Validated @ModelAttribute ReqResExamItemUpdate reqResExamItemUpdate){

		ResBoolSimpleInfo res = resourceExamItemService.updateExamItem(reqResExamItemUpdate);
		return ReturnResult.success(res);
	}

	
	@ApiOperation(value="删除试卷明细")
	@ApiImplicitParam(name = "examItemId", value = "试卷明细主键", paramType="query", required = true)
	@RequestMapping(value="/delExamItem",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> delExamItem(
			@NotBlank (message="examItemId 不能为空") String examItemId){
		ResBoolSimpleInfo res = resourceExamItemService.delExamItem(examItemId);
		return ReturnResult.success(res);
	}

	@ApiOperation(value="更改试卷明细的题目顺序")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "origExamItemId", value = "试卷明细id", paramType="query", required = true),
			@ApiImplicitParam(name = "destExamItemId", value = "试卷明细id", paramType="query", required = true)}
	)
	@RequestMapping(value="/changeExamItemSort",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> changeExamItemSort(@NotBlank (message="origExamItemId 不能为空") String origExamItemId,
															  @NotBlank (message="destExamItemId 不能为空") String destExamItemId){

		ResBoolSimpleInfo res = resourceExamItemService.changeExamItemSort(origExamItemId, destExamItemId);
		return ReturnResult.success(res);
	}
}
